/*
 * @Author: lys
 * @Description: 
 * @Date: 2024-05-08 00:45:52
 * @LastEditors: lys
 * @LastEditTime: 2024-05-13 02:12:02
 */
const OSS = require('ali-oss')
import { v4 as uuidv4 } from 'uuid'
const OSSConfig = {
  uploadHost: 'https://wxdengji.oss-cn-hangzhou.aliyuncs.com', // OSS上传地址
  ossParams: {
    // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
    region: 'oss-cn-hangzhou',
    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    accessKeyId: 'LTAI5t5bQPXzyCajihMHWdCd',
    accessKeySecret: 'waoveShRnOwPNXt7vKeGOtyIiELlYU',
    // 填写Bucket名称（阿里云上的Bucket，也就是上传地址）。
    bucket: 'wxdengji'
  }
}
// 上传
function uploadOSS (file) {
  return new Promise(async (resolve, reject) => {
    const fileName = `${random_string(6)}_${file.name}`
    const client = new OSS({
      region: OSSConfig.ossParams.region,
      accessKeyId: OSSConfig.ossParams.accessKeyId,
      accessKeySecret: OSSConfig.ossParams.accessKeySecret,
      bucket: OSSConfig.ossParams.bucket
    })
    console.log('uuidv4', uuidv4());
    console.log('uuidv4', file.name);
    const res = await client.multipartUpload(fileName, file)
    // resolve(res)
    // 或者返回如下：
    resolve({
      fileUrl: `${OSSConfig.uploadHost}/${fileName}`,
      fileName: file.name
    })
  })
}
// 删除
function deleteOSS (fileName) {
  return new Promise(async (resolve, reject) => {
    // const fileName = `${random_string(6)}_${file.name}`
    const client = new OSS({
      region: OSSConfig.ossParams.region,
      accessKeyId: OSSConfig.ossParams.accessKeyId,
      accessKeySecret: OSSConfig.ossParams.accessKeySecret,
      bucket: OSSConfig.ossParams.bucket
    })
    client.useBucket("oss");
    const res = await client.delete(fileName)
    resolve(res)
  })
}
export { uploadOSS, deleteOSS }
